#!/bin/bash

# get the checker file
pathtolexer=$(cat ../filelist.json | grep "lexcheck.h" | tr -d '"' | tr -d ',' | sed 's/  *//' | sed 's/^\./\.\.\/\.\.\/\.\./')

# check that XSTR_ALL_ARGS contains all the options defined above

all_args0=$(cat $pathtolexer | sed -n '1,/XLEN_ALL_ARGS/p' | head -n -1 | grep "#define" | grep -v XRNL | grep -v XLEXCHECK_H | awk '{ print $2 }' | sed 's/^[^_]*[_]//' | sed 's/_//g' | sed 's/NULL//' | tr '[:upper:]' '[:lower:]')

all_args1=$(cat $pathtolexer | grep XSTR_ALL_ARGS | grep -v XSTR_ALL_ARGS_UNIQ | sed 's/#define  XSTR_ALL_ARGS //' | tr -d ',' | tr -d '"' | tr -d '{' | tr -d '}' | tr -d '-' | tr -s ' ' '\n' )

combined_strings=$(paste <(echo -e "$all_args0") <(echo -e "$all_args1"))

error_str=$(awk '{ if ($1 != $2) { print $1, $2 } }' <<< "$combined_strings")
if [ "$error_str" != "" ]; then
    echo "Error $error_str"
fi

# check XLEN_ALL_ARGS
len=$(echo "$all_args0" | wc -w); len=$(( len + 1 ))
len_xlen_all_args=$( cat $pathtolexer | grep XLEN_ALL_ARGS | grep '#define' | grep -v XLEN_ALL_ARGS_UNIQ | awk '{ print $3 }' )

if [ "$len" != "$len_xlen_all_args" ]; then
    echo "Error XLEN_ALL_ARGS seen $len_xlen_all_args != measured $len"
fi

# check XLEN_ALL_COMM

all_commands=$(cat $pathtolexer | grep XALL_COMM | sed 's/#define *XALL_COMM //' | tr -d ',' | tr -d '"' | tr -d '{' | tr -d '}' | tr -d '-' | tr -s ' ' '\n' | sed 's/^[^_]*[_]//' | sed 's/_//g' | tr '[:upper:]' '[:lower:]' | tail -n +2 )

len=$(echo "$all_commands" | wc -w)
len_xlen_all_comm=$( cat $pathtolexer | grep XLEN_ALL_COMM | grep '#define' | awk '{ print $3 }' )

if [ "$len" != "$len_xlen_all_comm" ]; then
    echo "Error XLEN_ALL_COMM seen $len_xlen_all_comm != measured $len"
fi

# check XLEN_ALL_ARGS_UNIQ

all_args_uniq=$(cat $pathtolexer | grep XSTR_ALL_ARGS_UNIQ | sed 's/#define *XSTR_ALL_ARGS_UNIQ //' | tr -d ',' | tr -d '"' | tr -d '{' | tr -d '}' | tr -d '-' | tr -s ' ' '\n' | sed 's/^[^_]*[_]//' | sed 's/_//g' | tr '[:upper:]' '[:lower:]' )

len=$(echo "$all_args_uniq" | wc -w)
len_xlen_all_args_uniq=$( cat $pathtolexer | grep XLEN_ALL_ARGS_UNIQ | grep '#define' | awk '{ print $3 }' )

if [ "$len" != "$len_xlen_all_args_uniq" ]; then
    echo "Error XLEN_ALL_ARGS_UNIQ seen $len_xlen_all_args_uniq != measured $len"
fi

# check XALL_COMM

all_commands1=$(cat $pathtolexer | grep XSTR_ALL_ARGS | grep -v XSTR_ALL_ARGS_UNIQ | sed 's/#define  XSTR_ALL_ARGS //' | tr -s ',' '\n' | tr -d '"' | tr -d '{' | tr -d '}' | grep -- '--' | sed 's/ --//' | sed '1s/^/null\n/' | tr -d '-' )

combined_strings=$(paste <(echo -e "$all_commands") <(echo -e "$all_commands1"))
error_str=$(awk '{ if ($1 != $2) { print $1, $2 } }' <<< "$combined_strings")
if [ "$error_str" != "" ]; then
    echo "Error $error_str"
fi

# check XSTR_ALL_ARGS_UNIQ

all_str_uniq=$(echo "$all_args0" | sort | uniq )
all_str_uniq2=$(echo "$all_args_uniq" | sort | uniq )

combined_strings=$(paste <(echo -e "$all_str_uniq") <(echo -e "$all_str_uniq2"))
echo $combined_strings > /tmp/trash # TODO remove
error_str=$(awk '{ if ($1 != $2) { print $1, $2 } }' <<< "$combined_strings")
if [ "$error_str" != "" ]; then
    echo "Error $error_str"
fi

# check XARG

xargstring=$(cat $pathtolexer | awk '/XARG.*XARG/{print $0}' | awk '{print $2}' | sed 's/^[^_]*[_]//' | tr -d '-'  | tr '[:upper:]' '[:lower:]' | tr -s ' ' '\n' | sed 's/null//' | tr -d '_' )

combined_strings=$(paste <(echo -e "$xargstring") <(echo -e "$all_args0"))
error_str=$(awk '{ if ($1 != $2) { print $1, $2 } }' <<< "$combined_strings")
if [ "$error_str" != "" ]; then
    echo "Error $error_str"
fi

# check increasing numbers

all_args_num=$(cat $pathtolexer | sed -n '1,/XLEN_ALL_ARGS/p' | head -n -1 | grep "#define" | grep -v XRNL | grep -v XLEXCHECK_H | awk '{ print $3 }')

i=0
for word in ${all_args_num[@]}; do
    if [ "$i" != "$word" ]; then
        echo "wrong index at $word $i"
    fi
    i=$(( i + 1 ))
done


